package cn.iocoder.yudao.module.book.controller.admin.posts;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.posts.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.posts.PostsDO;
import cn.iocoder.yudao.module.book.service.posts.PostsService;

@Tag(name = "管理后台 - 社区动态")
@RestController
@RequestMapping("/book/posts")
@Validated
public class PostsController {

    @Resource
    private PostsService postsService;

    @PostMapping("/create")
    @Operation(summary = "创建社区动态")
    @PreAuthorize("@ss.hasPermission('book:posts:create')")
    public CommonResult<Long> createPosts(@Valid @RequestBody PostsSaveReqVO createReqVO) {
        return success(postsService.createPosts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社区动态")
    @PreAuthorize("@ss.hasPermission('book:posts:update')")
    public CommonResult<Boolean> updatePosts(@Valid @RequestBody PostsSaveReqVO updateReqVO) {
        postsService.updatePosts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社区动态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:posts:delete')")
    public CommonResult<Boolean> deletePosts(@RequestParam("id") Long id) {
        postsService.deletePosts(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社区动态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:posts:query')")
    public CommonResult<PostsRespVO> getPosts(@RequestParam("id") Long id) {
        PostsDO posts = postsService.getPosts(id);
        return success(BeanUtils.toBean(posts, PostsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社区动态分页")
    @PreAuthorize("@ss.hasPermission('book:posts:query')")
    public CommonResult<PageResult<PostsRespVO>> getPostsPage(@Valid PostsPageReqVO pageReqVO) {
        PageResult<PostsDO> pageResult = postsService.getPostsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PostsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社区动态 Excel")
    @PreAuthorize("@ss.hasPermission('book:posts:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPostsExcel(@Valid PostsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PostsDO> list = postsService.getPostsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社区动态.xls", "数据", PostsRespVO.class,
                        BeanUtils.toBean(list, PostsRespVO.class));
    }

}